from django.http.request import HttpRequest
from django.http.response import StreamingHttpResponse, HttpResponse
from django.db.models import QuerySet

from app import site
from . import models
from app.write2excel import WriteExcel

from django.contrib import admin


class BusinessDeptAdmin(admin.ModelAdmin):
    actions = ['download_report']

    def download_report(self, request:HttpRequest, queryset:QuerySet):
        wexcel = WriteExcel(queryset.first())
        wexcel.write_bdept2excel()

        response = HttpResponse()
        response['Content-Type'] = 'application/vnd.ms-excel'
        response['Content-Disposition'] = 'attachment;filename="report.xls"' #.format(wexcel.filename)

        wexcel.write2stream(response)

        return response

    download_report.short_description = '下载报告(单个)'


site.register(models.Company)
site.register(models.BusinessDept, BusinessDeptAdmin)
site.register(models.BusinessLine)
site.register(models.Employee)
